import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert' show json;

import 'package:flutter/services.dart';
import 'package:rongcloud_im_plugin/rongcloud_im_plugin.dart';
import 'package:rongcloud_im_plugin/rc_common_define.dart';
import 'package:rongcloud_im_plugin/text_message.dart';
import 'package:rongcloud_im_plugin/image_message.dart';
import 'package:rongcloud_im_plugin/file_message.dart';
import 'package:rongcloud_im_plugin/voice_message.dart';
import 'package:rongcloud_im_plugin/message.dart';
import 'package:rongcloud_im_plugin/message_factory.dart';
import 'package:rongcloud_im_plugin/conversation.dart';
import 'package:rongcloud_im_plugin/rc_status_define.dart';
import 'package:rongcloud_im_plugin/chatroom_info.dart';

import 'test_message.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static const String privateUserId = "kfsp585102706377490432";
  static const Map user = {
    "id": "kfsp591209067427397632",
    "name": "17000000002",
    "portrait": "https://res-kfsp-test.kefang.net/system/default/avatar"
  };

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    //融云 appkey
    String RongAppKey = '8brlm7uf8zf63';
    //用户 id
    String userId = 'kfsp591209067427397632';
    //通过用户 id 生成的对应融云 token
    String RongIMToken =
        '2GsdtrWvd0lvLTceDpmn/ZvXjjvTGzJSNrIVv8Ilag/u56862cF6SQeo7DIobYvJYocftwrxOsNvOhPwBdNEe4ONDr3dksQumhsXzTmb0QJ7GhsqM2sZzw==';

    //1.初始化 im SDK
    RongcloudImPlugin.init(RongAppKey);

    //2.配置 im SDK
    String confString = await DefaultAssetBundle.of(context)
        .loadString("assets/RCFlutterConf.json");
    Map confMap = json.decode(confString.toString());
    RongcloudImPlugin.config(confMap);

    //设置导航服务器和上传文件服务器信息
    //必须在 init 之后 ，connect 之前调用
    //1、如果使用https，则设置为https://cn.xxx.com:port或https://cn.xxx.com格式，其中域名部分也可以是IP，如果不指定端口，将默认使用443端口。
    //2、如果使用http，则设置为cn.xxx.com:port或cn.xxx.com格式，其中域名部分也可以是IP，如果不指定端口，将默认使用80端口。
    // String naviServer = "";
    // String fileServer = "";
    // RongcloudImPlugin.setServerInfo(naviServer, fileServer);

    //3.连接 im SDK
    int rc = await RongcloudImPlugin.connect(RongIMToken);
    print('connect result');
    print(rc);

    //4.刷新当前用户的用户信息
    String portraitUrl = "https://www.rongcloud.cn/pc/images/huawei-icon.png";
    RongcloudImPlugin.updateCurrentUserInfo(userId, "李四", portraitUrl);

    //5.设置监听回调，处理 native 层传递过来的事件
    RongcloudImPlugin.setRCNativeMethodCallHandler(_handler);

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.

    setState(() {});
  }

  //6.响应 native 的事件
  Future<dynamic> _handler(MethodCall methodCall) {
    //当 im SDK 需要展示用户信息的时候，会回调此方法
    if (RCMethodCallBackKey.RefrechUserInfo == methodCall.method) {
      //开发者需要将用户信息传递给 SDK
      //如果本地有该用户的信息，那么直接传递给 SDK
      //如果本地没有该用户的信息，从 APP 服务获取后传递给 SDK
      String userId = methodCall.arguments;
      String name = "张三";
      String portraitUrl = "https://www.rongcloud.cn/pc/images/lizhi-icon.png";
      RongcloudImPlugin.refreshUserInfo(userId, name, portraitUrl);
    } else if (RCMethodCallBackKey.ReceiveMessage == methodCall.method) {
      //收到消息原生会触发此方法
      Map map = methodCall.arguments;
      print("messageMap=" + map.toString());
      int left = map["left"];
      print("left=" + left.toString());
      String messageString = map["message"];
      Message msg = MessageFactory.instance.string2Message(messageString);

      //会话类型，单聊/群聊/聊天室
      print("conversationType = " + msg.conversationType.toString());
      print("targetId = " + msg.targetId);
      print("senderUserId=" + msg.senderUserId);
      onGetConversationList();
    } else if (RCMethodCallBackKey.SendMessage == methodCall.method) {
      //发送消息会触发此回调，通知 flutter 层消息发送结果
      // {"messageId":12,"status":30,"code":0}
      // messageId 为本地数据库自增字段
      // status 结果参见 RCMessageSentStatus 的枚举值
      // code 错误码，消息发送成功为 0 ，发送失败则为具体错误码 如 23408
      Map map = methodCall.arguments;
      print("message sent result " + map.toString());
    } else if (RCMethodCallBackKey.JoinChatRoom == methodCall.method) {
      //加入聊天室的回调
      //targetId 聊天室 id
      //status 参见 RCOperationStatus
      //{"targetId":targetId,"status":0};
      Map map = methodCall.arguments;
      print("join chatroom resulut =" + map.toString());
    } else if (RCMethodCallBackKey.QuitChatRoom == methodCall.method) {
      //退出聊天室的回调
      //targetId 聊天室 id
      //status 参见 RCOperationStatus
      //{"targetId":targetId,"status":0};
      Map map = methodCall.arguments;
      print("quit chatroom resulut =" + map.toString());
    } else if (RCMethodCallBackKey.UploadMediaProgress == methodCall.method) {
      //上传图片进度的回调
      //{"messageId",messageId,"progress",99}
      Map map = methodCall.arguments;
      print("upload image message progress = " + map.toString());
    }
  }

  onSendMessage() async {
    TextMessage txtMessage = new TextMessage();
    txtMessage.content = "这条消息来自 flutter";
    txtMessage.user = user;
    Message msg = await RongcloudImPlugin.sendMessage(
        RCConversationType.Private, privateUserId, txtMessage);
    print("send message start senderUserId = " + msg.senderUserId);
  }

  onSendImageMessage() async {
    ImageMessage imgMessage = new ImageMessage();
    imgMessage.localPath = "image/local/path.jpg";
    Message msg = await RongcloudImPlugin.sendMessage(
        RCConversationType.Private, privateUserId, imgMessage);
    print("send image message start senderUserId = " + msg.senderUserId);
  }

  onSendVoiceMessage() async {
    VoiceMessage voiceMsg = new VoiceMessage();
    voiceMsg.localPath = "voice/local/path";
    voiceMsg.duration = 13;
    Message msg = await RongcloudImPlugin.sendMessage(
        RCConversationType.Private, privateUserId, voiceMsg);
    if (msg != null) {
      print("send voice message start senderUserId = " + msg.senderUserId);
    }
  }

  onSendTestMessage() async {
    TestMessage testMessage = new TestMessage();
    testMessage.content = "这条消息是 flutter 内自定义的消息，还需要再原生的页面注册";
    Message msg = await RongcloudImPlugin.sendMessage(
        RCConversationType.Private, privateUserId, testMessage);
    print("send test message start senderUserId = " + msg.senderUserId);
  }

  onGetHistoryMessages() async {
    List msgs = await RongcloudImPlugin.getHistoryMessage(
        RCConversationType.Private, privateUserId, 0, 10);
    print("get history message");
    for (Message m in msgs) {
      print("sentTime = " + m.sentTime.toString());
    }
  }

  onGetConversationList() async {
    List cons = await RongcloudImPlugin.getConversationList();
    for (Conversation con in cons) {
      print("conversation latestMessageId " + con.latestMessageId.toString());
    }
  }

  onJoinChatRoom() {
    RongcloudImPlugin.joinChatRoom("testchatroomId", 10);
  }

  onQuitChatRoom() {
    RongcloudImPlugin.quitChatRoom("testchatroomId");
  }

  onGetChatRoomInfo() async {
    ChatRoomInfo chatRoomInfo = await RongcloudImPlugin.getChatRoomInfo(
        "testchatroomId", 10, RCChatRoomMemberOrder.Desc);
    print("onGetChatRoomInfo targetId =" + chatRoomInfo.targetId);
  }

  // 加入黑名单
  addToBlacklist() async {
    bool rs = await RongcloudImPlugin.addToBlacklist("kfsp601833350369378304");

    print(rs);
  }

  // 从黑名单移除
  removeFromBlacklist() async {
    bool rs =
        await RongcloudImPlugin.removeFromBlacklist("kfsp601833350369378304");

    print(rs);
  }

  // 查询用户是否在黑名单中
  getBlacklistStatus() async {
    bool rs =
        await RongcloudImPlugin.getBlacklistStatus("kfsp601833350369378304");

    print(rs);
  }

  // 获取黑名单列表
  getBlacklist() async {
    List rs = await RongcloudImPlugin.getBlacklist();

    print(rs);
  }

  @override
  Widget build(BuildContext context) {
    // return MaterialApp(
    //   title: 'Flutter Demo',
    //   theme: ThemeData(
    //     primarySwatch: Colors.blue,
    //   ),
    //   home: IndexPage(),
    // );
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 10),
            height: 500,
            child: LayoutBuilder(
              builder:
                  (BuildContext context, BoxConstraints viewportConstraints) {
                return SingleChildScrollView(
                  child: ConstrainedBox(
                    constraints: BoxConstraints(
                      minHeight: viewportConstraints.maxHeight,
                    ),
                    child: Column(
                      children: <Widget>[
                        Row(children: <Widget>[]),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => addToBlacklist(),
                                    child: Text("加入黑名单"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => removeFromBlacklist(),
                                    child: Text("从黑名单移除"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => getBlacklistStatus(),
                                    child: Text("查询用户是否在黑名单中"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => getBlacklist(),
                                    child: Text("获取黑名单列表"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => onSendMessage(),
                                    child: Text("sendMessage"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => onSendVoiceMessage(),
                                    child: Text("onSendVoiceMessage"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => onSendTestMessage(),
                                    child: Text("onSendTestMessage"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => onGetHistoryMessages(),
                                    child: Text("onGetHistoryMessages"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => onGetConversationList(),
                                    child: Text("onGetConversationList"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => onJoinChatRoom(),
                                    child: Text("onJoinChatRoom"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            )),
                        Padding(
                            padding: EdgeInsets.symmetric(vertical: 10),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  child: RaisedButton(
                                    onPressed: () => onGetChatRoomInfo(),
                                    child: Text("onGetChatRoomInfo"),
                                    color: Colors.blueAccent,
                                    textColor: Colors.white,
                                  ),
                                )
                              ],
                            ))
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
